package stz.flash.option.app;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import stz.flash.option.OptionLabel;
import stz.flash.option.Option;
import stz.flash.string.OptionInformation;
import stz.flash.string.OptionString;
import blackberry.util.field.DjChoiceField;
import blackberry.util.field.DjLabelField;


public class StartupModeChoiceField extends DjChoiceField implements FieldChangeListener {

	public static final String[] CHOICES = new String[] {
		OptionString.OPTION_CHOICE_FIELD_MODE_LATEST, 
		OptionString.OPTION_CHOICE_FIELD_MODE_FLASHLIGHT, 
		OptionString.OPTION_CHOICE_FIELD_MODE_STROBE};
	
	public StartupModeChoiceField() {
		super(DjLabelField.createSimpleLabel(OptionString.STARTUP_MODE_CHOICE_FIELD),
				OptionLabel.createInfoLabel(OptionInformation.STARTUP_MODE_CHOICE_FIELD),
				CHOICES);
		initSelected();
		setChangeListener(this);
	}
	
	protected void initSelected() {
		
		if (Option.getIns().getSelectedStartupMode() == OptionString.OPTION_CHOICE_FIELD_MODE_LATEST) {
			setSelectedIndex(OptionString.OPTION_CHOICE_FIELD_MODE_LATEST);
		} else if (Option.getIns().getSelectedStartupMode() == OptionString.OPTION_CHOICE_FIELD_MODE_FLASHLIGHT) {
			setSelectedIndex(OptionString.OPTION_CHOICE_FIELD_MODE_FLASHLIGHT);
		} else if (Option.getIns().getSelectedStartupMode() == OptionString.OPTION_CHOICE_FIELD_MODE_STROBE) {
			setSelectedIndex(OptionString.OPTION_CHOICE_FIELD_MODE_STROBE);
		}
	}
	
	public void fieldChanged(Field field, int context) {
		if (context == 0 || context == PROGRAMMATIC)
			return;
		if (getChoice(getSelectedIndex()) == OptionString.OPTION_CHOICE_FIELD_MODE_LATEST) {
			Option.getIns().setSelectedStartupMode(OptionString.OPTION_CHOICE_FIELD_MODE_LATEST);
		} else if (getChoice(getSelectedIndex()) == OptionString.OPTION_CHOICE_FIELD_MODE_FLASHLIGHT) {
			Option.getIns().setSelectedStartupMode(OptionString.OPTION_CHOICE_FIELD_MODE_FLASHLIGHT);
		} else if (getChoice(getSelectedIndex()) == OptionString.OPTION_CHOICE_FIELD_MODE_STROBE) {
			Option.getIns().setSelectedStartupMode(OptionString.OPTION_CHOICE_FIELD_MODE_STROBE);
		}
	}
}